<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <treeTools :tree-node="company" :is-root="true" @delDepts="getDepartments" @addDepts="addDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          node-key="id"
          :default-expand-all="true"
        >
          <treeTools slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @editDepts="editDepts" @addDepts="addDepts" />
        </el-tree>
      </el-card>

    </div>
    <AddDept ref="addDept" :tree-node="node" :show-dialog="showDialog" @addDepts="getDepartments" @close="showDialog = false" />
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils/auth'
import AddDept from '@/views/departments/components/add-dept.vue'
export default {
  components: {
    treeTools, AddDept
  },
  data() {
    return {
      company: {},
      departs: [{ name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        { name: '行政部', manager: '刘备' },
        { name: '人事部', manager: '孙权' }],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      const list = transListToTreeData(res.depts, '')
      this.departs = list
      this.company = { name: res.companyName, manager: '负责人', id: '' }
    },
    addDepts(node) {
      this.node = node
      this.showDialog = true
    },
    editDepts(node) {
      this.node = node
      this.showDialog = true
      this.$refs.addDept.getDepartDetail(node.id)
    }

  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
